package bank;

import iban.*;

import java.util.Date;
import java.util.Random;

/**
 * 
 */

/**
 * @author Dennis Post
 * 
 */
public abstract class BankAccount {

	protected Customer customer;
	protected Date creationDate;
	protected Employer employer;
	protected String bic;
	protected String iban;
	protected float currentSaldo;
	protected float maxDispo;
	protected int currency; // 0 = € Euro, 1 = US$ US Dollar, 2 = £ brit. Pfund

	public BankAccount(Customer cust, Date creationDate, Employer employer,
			String blz) {
		this(cust, creationDate, employer, blz, 0);
	}
	
	public BankAccount(Customer cust, Date creationDate, Employer employer,
			String blz, int currency) {
		this.customer = cust;
		this.creationDate = creationDate;
		this.bic = Constants.BIC;
		this.maxDispo = 0;
		this.employer = employer;
		this.currency = currency;
		
		Iban iban = new Iban.Builder()
    		.countryCode(CountryCode.DE)
    		.bankCode(Constants.BLZ)
    		.accountNumber(generateAccountNumberAsString())
    		.build();
		this.iban = iban.toString();

	}
	
	private String generateAccountNumberAsString() {
		
		return "1023425644";
		
	}
	
	public boolean deposit(float value) {
		try {
			setNewSaldoWithDiff(value);
			return true;
		} catch (NumberFormatException nfEx) {
			return false;
		}
	}

	public boolean withdraw(float value) {
		try {
			setNewSaldoWithDiff(value);
			return true;
		} catch (NumberFormatException nfEx) {
			return false;
		}
	}

	public boolean transferValueTo(float value, BankAccount bankAccount) {

		if ((this.currentSaldo - value) > this.maxDispo) {
			setNewSaldoWithDiff(value);
			bankAccount.setNewSaldoWithDiff(value);
			return true;
		} else {
			System.out.println("Guthaben reicht nicht aus!");
		}
		return false;
	}

	public boolean transferOwnershipTo(Customer newCustomer) {
		return false;
	}

	
	
	/* GETTERS */
	public BankAccount getBankAccount() {

		// get griokonto from database;

		return this;
	}

	public float getCurrentSaldo() {

		return this.currentSaldo;

	}

	public float getCurrentSaldoInEuro() {

		// 1€ = 1.3863$
		switch (this.currency) {
		case 0:
			return getCurrentSaldo();
		case 1:
			return getCurrentSaldo() * 1.3863f;
		default:
			return getCurrentSaldo();
		}

	}

	public float getCurrentSaldoInDollar() {
		
		// 1$ = 0.7213€
		switch (this.currency) {
		case 0:
			return getCurrentSaldo() * 0.7213f;
		case 1:
			return getCurrentSaldo();
		default:
			return getCurrentSaldo();
		}

	}

	public float getCurrentDispo() {

		return this.maxDispo;

	}

	/* SETTERS */

	public boolean setNewDispo(float newDispo) {
		try {
			this.maxDispo = newDispo;
			System.out.println("Dispo für " + this.iban + " wurde auf " + newDispo + " gesetzt.");
			return true;
		} catch (NumberFormatException nfEx) {
			return false;
		}
	}

	public boolean setNewSaldoWithDiff(float value) {
		try {
			this.currentSaldo = this.currentSaldo + value;
			return true;
		} catch (NumberFormatException nfEx) {
			return false;
		}
	}
}
